home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / grafica / ghostscript / 5.10 / data / ps2epsi.ps < prev    next >
Text File  |  1997-09-29  |  8KB  |  254 lines

  1. %    Copyright (C) 1990, 1995, 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Convert an arbitrary PostScript file to an EPSI file.
  16. %
  17. % Original version contributed by
  18. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  19. % Patched 7/26/95 by
  20. %    Greg P. Kochanski <gpk@bell-labs.com>
  21. %   to add many new DSC comments and make the comments conforming.
  22. % Bug fix 9/29/97 by lpd <ghost@aladdin.com>: if the page size wasn't an
  23. %   exact multiple of 8 bits, an incorrect bounding box (or a rangecheck
  24. %   error) could occur.
  25. % Please do not contact these users if you have questions.  They no longer
  26. % have the time, interest, or current expertise to keep this code working.
  27. % If you find bugs, please send proposed fixes to ghost@aladdin.com.
  28. %
  29.  
  30. % Initialize, and redefine copypage and showpage.
  31.  
  32. % ps2edict is defined in the pre-loaded code created by the ps2epsi script.
  33. % /ps2edict 25 dict def
  34. ps2edict begin
  35.  
  36.                 % The main procedure
  37.   /ps2epsi
  38.    {                % Open the file
  39.      outfile (w) file /epsifile exch def
  40.                     % Get the device parameters
  41.      currentdevice getdeviceprops .dicttomark
  42.      /HWSize get aload pop
  43.        /devheight exch def
  44.        /devwidth exch def
  45.      matrix defaultmatrix
  46.        /devmatrix exch def
  47.                 % Make a corresponding memory device
  48.      devmatrix devwidth devheight <ff 00>
  49.      makeimagedevice
  50.      /arraydevice exch def
  51.      arraydevice setdevice    % (does an erasepage)
  52.      /rowwidth devwidth 7 add 8 idiv def
  53.      /row rowwidth string def
  54.                 % Replace the definition of showpage
  55.      userdict /showpage { ps2edict begin epsipage end } bind put
  56.      userdict /setfont { ps2edict begin epsisetfont end } bind put
  57.    } def
  58.  
  59.  /epsifontdict 100 dict def
  60.  
  61.  /epsisetfont
  62.  {
  63.  % code here keeps a list of font names in dictionary epsifontdict
  64.  /tmpfont exch def
  65.  /tmpfontname tmpfont /FontName get def
  66.  epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
  67.  epsifontdict tmpfontname
  68.     epsifontdict tmpfontname get 1 add put
  69.  tmpfont setfont
  70.  } bind def
  71.  
  72. % Get a scan line from the memory device, zeroing any bits beyond
  73. % the device width.
  74.  /getscanline        % <device> <y> <string> getscanline -
  75.  {
  76.     dup 4 1 roll copyscanlines pop
  77.     16#ff00 devwidth 7 and neg bitshift 255 and
  78.     dup 0 ne {
  79.       exch dup length 1 sub 2 copy get 4 -1 roll and put
  80.     } {
  81.       pop pop
  82.     } ifelse
  83.  } bind def
  84.  
  85.  /margintest
  86.  {
  87.       { dup arraydevice exch row getscanline
  88.     -1 0 1 rowwidth 1 sub
  89.     { dup row exch get 0 ne { exit }{ pop } ifelse
  90.     } for
  91.     dup -1 ne { exch pop } if
  92.     -1 ne { exit } if pop
  93.       } for
  94.  } bind def
  95.  
  96.  
  97.   /epsiNameStr 200 string def
  98.   /epsiNpages 0 def
  99.   /epsiNpageStr 20 string def
  100.   /epsipage
  101.    {
  102.      /epsiNpages epsiNpages 1 add def
  103.      /loopcount devheight 1 sub def
  104.  
  105.      % Find top margin
  106.      -1 0 1 loopcount margintest
  107.      dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse 
  108.      /tm exch def
  109.  
  110.      % Find bottom margin
  111.      loopcount -1 0 margintest
  112.      /bm exch def
  113.      
  114.      % Initialise limit variables
  115.      /loopcount rowwidth 1 sub def
  116.      /lm loopcount def /lmb 0 def
  117.      /rm 0 def /rmb 0 def
  118.  
  119.      % Find left and right boundaries of image
  120.      tm 1 bm
  121.       { % Get more data
  122.     arraydevice exch row getscanline
  123.     % Scan from left to find first non-zero element
  124.     % We save first the element, then the index
  125.     -1 0 1 loopcount
  126.     { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  127.     } for
  128.     % If we found -1, row is blank ..
  129.     dup -1 ne 
  130.     { % Find the leftmost index
  131.           dup lm lt
  132.           % If the new index is less, we save index and element
  133.           { /lm exch def /lmb exch def }
  134.           % If the index is equal, we or the bits together
  135.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  136.           } ifelse
  137.       % Now find the rightmost index
  138.       loopcount -1 0
  139.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  140.           } for
  141.       dup rm gt
  142.           % If the new index is greater, we save index and element
  143.           { /rm exch def /rmb exch def }
  144.           % If the index is equal, or the bits
  145.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  146.           } ifelse
  147.     } if
  148.     pop
  149.       } for
  150.  
  151.      % Now we find the real left & right bit positions
  152.      256 0 1 7
  153.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  154.      } for
  155.      /lmb exch def
  156.  
  157.      1 7 -1 0
  158.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  159.      } for
  160.      /rmb exch def
  161.  
  162.      % Calculate the bounding box values
  163.      /llx lm 8 mul lmb add  1 sub def
  164.      /lly devheight bm sub  1 sub def
  165.      /urx rm 8 mul rmb add 1 add def
  166.      /ury devheight tm sub 1 add def
  167.  
  168.     % Write out the magic string and bounding box information
  169.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  170.      epsifile epsititle writestring
  171.      epsifile epsicreator writestring
  172.      epsifile epsicrdt writestring
  173.      epsifile epsifor writestring
  174.      epsifile flushfile
  175.  
  176.     % Write out the page count:
  177.      epsifile (%%Pages: ) writestring
  178.      epsifile epsiNpages epsiNpageStr cvs writestring
  179.      epsifile (\n) writestring
  180.      epsifile flushfile
  181.  
  182.     % Write out the list of used fonts:
  183.      epsifile (%%DocumentFonts:) writestring
  184.      epsifontdict {
  185.                     epsifile ( ) writestring
  186.                     pop epsiNameStr cvs epsifile exch writestring
  187.                     } forall
  188.      epsifile (\n) writestring
  189.      epsifile flushfile
  190.  
  191.      epsifile (%%BoundingBox: ) writestring
  192.      epsifile llx write==only epsifile ( ) writestring
  193.      epsifile lly write==only epsifile ( ) writestring
  194.      epsifile urx write==only epsifile ( ) writestring
  195.      epsifile ury write==
  196.      epsifile (%%BeginPreview: ) writestring
  197.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  198.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  199.      epsifile bm tm sub 1 add write==
  200.      epsifile flushfile
  201.  
  202.     % Define character and bit widths for the output line buffer:
  203.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  204.      /bwidth cwidth 8 mul def
  205.      /owidth urx llx sub 1 add 7 add 8 idiv def
  206.      /out cwidth string def
  207.  
  208.      % Create a 1-bit-high device for bitblt to align with the bbox
  209.      gsave
  210.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice setdevice
  211.  
  212.      % 'image' a zero string to clear the line device
  213.      bwidth 1 1 matrix cwidth string image
  214.  
  215.      tm 1 bm
  216.       { % Get a scan line interval from the array device
  217.     arraydevice exch row copyscanlines lm cwidth getinterval
  218.     lmb 0 gt
  219.     { % 'image' it into the line device with the lmb offset
  220.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll image
  221.       % Now we get the modified scan line
  222.       currentdevice 0 out copyscanlines 0 owidth getinterval
  223.     } if
  224.     % Write out the hex data
  225.     epsifile (% ) writestring 
  226.     epsifile exch writehexstring
  227.     epsifile (\n) writestring
  228.       } for
  229.  
  230.      epsifile (%%EndImage\n) writestring
  231.      epsifile (%%EndPreview\n) writestring
  232.      epsifile flushfile
  233.      grestore
  234.      erasepage initgraphics
  235.  
  236.      DonePage 0 1 put
  237.    } bind def
  238.  
  239.  
  240. (outfile) getenv
  241.   { /outfile exch def 
  242.     ps2epsi
  243.  
  244.     /DonePage 1 string def
  245.     (%stdin) (r) file cvx execute0
  246.     DonePage 0 get 0 eq { showpage } if
  247.   } if
  248.  
  249. end
  250. quit
  251.